package com.nursinghome.admin.service.impl;

import com.nursinghome.admin.mapper.AccountMapper;
import com.nursinghome.admin.service.AccountService;
import com.nursinghome.bean.Account;
import com.nursinghome.bean.Elderly;
import com.nursinghome.exception.NurisingHomeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public Boolean selectByEname(String data, Long eid) {
        return accountMapper.selectByEname(data,eid)==null;
    }

    @Override
    public List<Account> selectByadata(String data) {
        return accountMapper.selectByadata(data);
    }

    @Override
    public Boolean insert(Account account) throws NurisingHomeException {
        //判断当前月份该老头是否有账单
        Date adata = account.getAdata();
        SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd");
        String data = sft.format(adata);
        if (accountMapper.selectByEname(data, account.getEid())!=null) {
            throw new NurisingHomeException("该老头该月已经有账单了，无法重复添加");
        }
        return accountMapper.insert(account)==1;
    }

    @Override
    public Boolean updata(Account account) throws NurisingHomeException {
        //判断当前账单是否存在
        Long aid = account.getAid();
        if (accountMapper.selectByAid(aid) == null) {
            throw new NurisingHomeException("该账单不存在，无法修改！");
        }
        return accountMapper.updata(account) == 1;
    }

    @Override
    public Account selectByAid(Long aid) {
        return accountMapper.selectByAid(aid);
    }

    @Override
    public List<Elderly> allElderly() {
        return accountMapper.allElderly();
    }
}
